package cn.topevery.garden.wapi.controller;

import cn.topevery.garden.dal.logic.EventInfoUrgeLogic;
import cn.topevery.garden.entity.dto.EventInfoUrgeDTO;
import cn.topevery.garden.entity.query.EventInfoUrgeQuery;
import cn.topevery.garden.entity.vo.EventInfoUrgeVO;
import cn.topevery.garden.entity.po.EventInfoUrge;
import hw.topevery.basis.entity.po.SysUser;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.util.ExcelUtil;
import hw.topevery.framework.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import com.github.dozermapper.core.Mapper;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

/**
 * 案件督办
 */
@Api(tags = "案件督办")
@RestController
@RequestMapping("/eventInfoUrge")
public class EventInfoUrgeController extends BaseController {

    @Autowired
    private Mapper mapper;

    @Autowired
    EventInfoUrgeLogic eventInfoUrgeLogic;


    /**
     * 获取案件督办分页列表
     *
     * @param query
     * @return
     */
    @ApiOperation(value = "获取案件督办页列表")
    @PostMapping("/listPage")
    public JsonResult<DataPager<EventInfoUrgeVO>> listPage(@RequestBody EventInfoUrgeQuery query) {
        return JsonResult.ok(eventInfoUrgeLogic.listPage(query));
    }

    @ApiOperation("新增/编辑 案件督办信息")
    @PostMapping("/saveOrUpdate")
    public JsonResult<Boolean> saveOrUpdate(@Valid @RequestBody EventInfoUrgeDTO dto) {
        SysUser user = getUser();
        if(dto.getId()==null || dto.getId().equals(0)){
            dto.setEntityId(UUID.randomUUID().toString());
            dto.setUserId(user.userId);
            dto.setUserName(user.userName);
            dto.setDate(LocalDateTime.now());
        }
        eventInfoUrgeLogic.saveOrUpdate(user.id.toString(),mapper.map(dto, EventInfoUrge.class));
        return JsonResult.ok(true);
    }

    @ApiOperation("通过Id获取详情")
    @GetMapping("/getInfoById")
    public JsonResult<EventInfoUrgeVO> getInfoById(@RequestParam Integer id) {
        EventInfoUrge entity = eventInfoUrgeLogic.getEntity(id);
        EventInfoUrgeVO vo= mapper.map(entity, EventInfoUrgeVO.class);
        return JsonResult.ok(vo);
    }

    @ApiOperation("根据自增ID批量删除")
    @PostMapping("/deleteBatchById")
    public JsonResult<Boolean> deleteBatchById(@RequestBody Integer[] ids) {
        eventInfoUrgeLogic.deleteBatchById(getUser().id.toString(),ids);
        return JsonResult.ok(true);
    }

    @ApiOperation("导出案件督办信息")
    @GetMapping("/export")
    public void export(EventInfoUrgeQuery query, HttpServletResponse response) {
        List<EventInfoUrgeVO> list = eventInfoUrgeLogic.listPage(query).records;
        ExcelUtil.exportExcel(list, "案件督办", "sheet1", EventInfoUrgeVO.class, "案件督办", response);
    }

}
